package com.example.myapplication;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
/*
注册界面
 */
public class RegisterActivity extends AppCompatActivity {
    DBOpenHelper dbOpenHelper;
    SQLiteDatabase db;
    private TextView text;
    private EditText rgistEditAccount;
    private EditText registEditPassword;
    private EditText regisEditSex;
    private Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        dbOpenHelper=new DBOpenHelper(this,"users.db",null,2);
        db=dbOpenHelper.getWritableDatabase();
        initView();
        //注册按钮点击事件
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getfields()==3){
                    ContentValues values=new ContentValues();
                    values.put("username",rgistEditAccount.getText().toString());
                    values.put("password",registEditPassword.getText().toString());
                    db.insert("user",null,values);
                    values.clear();
                    startActivity(new Intent(RegisterActivity.this,DetailsActivity.class));
                    finish();
                }else if (getfields()==4){
                    regisEditSex.setVisibility(View.VISIBLE);
                    ContentValues values=new ContentValues();
                    values.put("username",rgistEditAccount.getText().toString());
                    values.put("password",registEditPassword.getText().toString());
                    values.put("sex",regisEditSex.getText().toString());
                    db.insert("user",null,values);
                    values.clear();
                    startActivity(new Intent(RegisterActivity.this,DetailsActivity.class));
                    finish();
                }
            }
        });
    }
   /*
    初始化控件
     */

    private void initView() {
        text = (TextView) findViewById(R.id.text);
        rgistEditAccount = (EditText) findViewById(R.id.rgist_edit_account);
        registEditPassword = (EditText) findViewById(R.id.regist_edit_password);
        regisEditSex = (EditText) findViewById(R.id.regis_edit_sex);
        btnRegister = (Button) findViewById(R.id.btn_register);

        //  判断字段数量，为3进行隐藏，字段数量为4不隐藏
        if (getfields()==3){
            regisEditSex.setVisibility(View.GONE);
        }else if (getfields()==4){
            regisEditSex.setVisibility(View.VISIBLE);
        }
    }
    /*
   得到数据库的字段，用于判断是否隐藏editSex文本框。
   字段数量为3进行隐藏，字段数量为4不隐藏
    */
    public int getfields(){
        Cursor cursor=db.query("user",null,null,null,null,null,null);
        int num=  cursor.getColumnCount();
        cursor.close();
        return num;
    }
}
